<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="add"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table v-loading="loading" :data="list">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template v-slot="scope">
                  <el-button
                    size="small"
                    type="success"
                    @click="fenpei(scope.row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="editor(scope.row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="Deleteroles(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :page="page.page"
                :page-size="page.pagesize"
                :total="total"
                layout="prev,pager,next"
                @current-change="change"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-form
              ref="ruleForm"
              :model="ruleForm"
              label-width="100px"
              class="demo-ruleForm"
            >
              <el-form-item label="企业名称">
                <el-input v-model="ruleForm.name" disabled />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="ruleForm.companyAddress" disabled />
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input v-model="ruleForm.companyPhone" disabled />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="ruleForm.mailbox" disabled />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="ruleForm.remarks" disabled /> </el-form-item></el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹框 -->
      <bou
        ref="com"
        :row="row"
        :is-all.sync="isAll"
        :state.sync="state"
        @zz="getlist"
        @gx="getlist"
      />
      <!--  -->
      <!-- 分配权限 -->
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        <el-tree
          ref="sp"
          :check-strictly="true"
          :data="data"
          show-checkbox
          node-key="id"
          :default-expand-all="true"
          :default-checked-keys="mo"
          :props="defaultProps"
        />
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="btnok">确 定</el-button>
        </span>
      </el-dialog>
      <!--  -->
    </div>
  </div>
</template>

<script>
// import { mapState } from 'vuex'
import { role, enterprise, Deleterole, editor, assignPerm } from '@/api/serring'
import { getPermissionList } from '@/api/quanxian'
import bou from './com/bounced.vue'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'VueAdminTemplateInde',
  components: {
    bou
  },
  data() {
    return {
      mo: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      roeid: null,
      data: [],
      dialogVisible: false,
      loading: false,
      row: {},
      list: [],
      isAll: null,
      id: '',
      page: {
        page: 1,
        pagesize: 10
      },
      ruleForm: {
        name: '',
        companyAddress: '',
        companyPhone: '',
        mailbox: '',
        remarks: ''
      },
      total: 0,
      state: false
    }
  },
  computed: {
    // ...mapState('user', {'UserInfo'})
  },

  created() {
    this.getlist()
    this.gitals()
  },

  methods: {
    // 获取企业信息
    async gitals() {
      this.id = this.$store.state.user.UserInfo.companyId
      const res = await enterprise(this.id)
      this.ruleForm = res
    },
    // 获取角色列表
    async getlist() {
      this.loading = true
      const res = await role(this.page)
      console.log(res.rows)
      this.list = res.rows
      this.total = res.total
      this.loading = false
    },
    // 分页
    change(val) {
      this.page.page = val
      this.getlist()
    },
    // 删除角色
    Deleteroles(id) {
      this.$confirm('此操作将永久删除改角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await Deleterole(id)
          console.log(res)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getlist()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 编辑
    async editor(id) {
      await this.getLists(id)
      this.isAll = true
      this.state = true
    },
    // 添加
    add() {
      this.isAll = false
      this.isa = ''
      this.state = true
    },
    // 获取编辑数据
    async getLists(id) {
      const res = await editor(id)
      console.log(res)
      this.row = res
    },
    // 分配权限关闭回调
    handleClose() {
      this.mo = []
      this.dialogVisible = false
    },
    // 点击分配权限
    async fenpei(id) {
      this.roeid = id
      await this.Accesspoint(id)
      this.dialogVisible = true
    },
    // 获取权限点
    async Accesspoint(id) {
      const res = await getPermissionList()
      const re = await tranListToTreeData(res)
      console.log(re)
      this.data = re
      const { permIds } = await editor(id)
      this.mo = permIds
    },
    async btnok() {
      this.mo = await this.$refs.sp.getCheckedKeys()
      console.log(this.mo)
      const res = await assignPerm({ id: this.roeid, permIds: this.mo })
      console.log(res)
      this.handleClose()
    }
  }
}
</script>

<style lang="scss" scoped>
//  .el-input__inner {
//   width: 350px !important;
//   height: 36px !important;
// }
::v-deep .el-input.is-disabled .el-input__inner {
  width: 350px !important;
}
::v-deep .el-form-item__label {
  font-weight: 400;
}
::v-deep .el-dialog__body {
  margin-right: 30px;
}
</style>
